package com.lonely.fx.table_view;

import com.lonely.fx.table_view.entity.SimplePerson;
import javafx.application.Application;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @date: 2022-08-02 11:03
 * @author: ztkj-hzb
 * @description: tableview 基础属性设置
 **/
public class TableViewBaseSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        AnchorPane anchorPane = new AnchorPane();

        HBox hBox = new HBox();

        Label nameLabel = new Label("修改name:");
        Label ageLabel = new Label("修改年龄：");

        TextField nameTf = new TextField();
        TextField ageTf = new TextField();

        hBox.getChildren().addAll(nameLabel, nameTf, ageLabel, ageTf);
        hBox.setSpacing(10);
        hBox.setAlignment(Pos.CENTER);
        hBox.prefWidthProperty().bind(anchorPane.widthProperty());


        //创建一个基于简单的javabean的tableview
        TableView<SimplePerson> tableView = new TableView<>();

        //设置宽高
        tableView.setPrefSize(600, 400);

        //设置数据
        ObservableList<SimplePerson> persons = FXCollections.observableArrayList();

        persons.add(new SimplePerson("小天", 20, "湖北省", false));
        persons.add(new SimplePerson("小地", 30, "湖南省", true));
        persons.add(new SimplePerson("小玄", 40, "广东省", false));
        persons.add(new SimplePerson("小黄", 50, "浙江省", true));
        tableView.setItems(persons);

        //创建列
        TableColumn<SimplePerson, String> nameTableCol = new TableColumn<>("用户名");
        TableColumn<SimplePerson, Number> ageTableCol = new TableColumn<>("年龄");
        TableColumn<SimplePerson, String> addressTableCol = new TableColumn<>("地址");
        TableColumn<SimplePerson, Boolean> marriedTableCol = new TableColumn<>("是否已婚");
        tableView.getColumns().add(nameTableCol);
        tableView.getColumns().add(1, ageTableCol);
        tableView.getColumns().addAll(addressTableCol, marriedTableCol);

        //设置列与数据绑定
        nameTableCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        addressTableCol.setCellValueFactory(new PropertyValueFactory<>("address"));
        ageTableCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<SimplePerson, Number>, ObservableValue<Number>>() {
            @Override
            public ObservableValue<Number> call(TableColumn.CellDataFeatures<SimplePerson, Number> param) {
                return new SimpleIntegerProperty(param.getValue().getAge());
            }
        });
        marriedTableCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<SimplePerson, Boolean>, ObservableValue<Boolean>>() {
            @Override
            public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<SimplePerson, Boolean> param) {
                return new SimpleBooleanProperty(param.getValue().getMarried());
            }
        });


        //设置动态添加显示列菜单
        tableView.setTableMenuButtonVisible(true);

        //当添加和移除列后触发该动作
        tableView.getVisibleLeafColumns().addListener((ListChangeListener<TableColumn<SimplePerson, ?>>) c -> System.out.println("可见列调整了"));

        //设置列宽度策略---这里设置为禁止拖动宽度
        //tableView.setColumnResizePolicy(param -> true);
        //默认效果
        //tableView.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
        //根据列均分
        tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        //设置单元格大小
        //tableView.setFixedCellSize(30);

        //设置单元格可编辑
        tableView.setEditable(true);
        nameTableCol.setCellFactory(TextFieldTableCell.forTableColumn());

        AnchorPane.setTopAnchor(hBox, 30.0);
        AnchorPane.setTopAnchor(tableView, 100.0);
        AnchorPane.setLeftAnchor(tableView, 100.0);
        anchorPane.getChildren().addAll(hBox, tableView);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(800);
        primaryStage.setHeight(600);
        primaryStage.show();

        nameTf.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if (event.getCode() != KeyCode.ENTER) {
                    return;
                }

                //修改name
                persons.get(0).setName(nameTf.getText());
                tableView.refresh();
            }
        });

        ageTf.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if (event.getCode() != KeyCode.ENTER) {
                    return;
                }

                //修改name
                persons.get(0).setAge(Integer.valueOf(ageTf.getText()));
                tableView.refresh();
            }
        });


        persons.addListener(new ListChangeListener<SimplePerson>() {
            @Override
            public void onChanged(Change<? extends SimplePerson> c) {

                System.out.println("aa");
                if (c.next()) {
                    System.out.println(c.wasUpdated());
                }
            }
        });

    }
}
